<template>
  <div>
    <Navigation />
    <HomePages />
    <div class="nav"></div>
    <div class="sub-filter commonwidth">
      <div class="item" v-for="(it, id) in filter.brands" :key="id">
        <div class="head">品牌</div>
        <div class="body">
          <span class="ellipsis">全部</span>
          <span class="ell">{{ it.name }}</span>
        </div>
      </div>
      <div
        class="itembox"
        v-for="(item, index) in filter.saleProperties"
        :key="index"
      >
        <div class="head">{{ item.name }}</div>
        <div class="body">
          <span class="ellipsis">全部</span>
          <span
            class="ell"
            v-for="(i, index) in item.properties"
            :key="index"
            >{{ i.name }}</span
          >
        </div>
      </div>
    </div>
    <div class="goods-list commonwidth">
      <div class="sub-sort">
        <div class="sort">
          <span class="sort-span active">默认排序</span>
          <span class="sort-span">最新排名</span>
          <span class="sort-span">最高人气</span>
          <span class="sort-span">评论最多</span>
          <span class="sort-span">价格排序</span>
        </div>
        <div class="cik">
          <div class="xtc">仅显示有货商品</div>
          <div class="xtcb">仅显示特惠商品</div>
        </div>
      </div>
      <ul>
        <li>
          <div
            class="goods-item"
            v-for="(item, index) in temporary.items"
            :key="index"
          >
            <img :src="item.picture" alt="" />
            <p class="name">{{ item.name }}</p>
            <p class="desc">{{ item.desc }}</p>
            <p class="price">{{ item.price }}</p>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import Navigation from "./Navigation.vue";
import HomePages from "@/components/HomePages.vue";

import axios from "axios";
export default {
  components: {
    HomePages,
    Navigation,
  },
  data() {
    return {
      filter: [],
      temporary: [],
      page: 1, //当前页
      items: 20,
      page_count: 16, // 总页数
    };
  },
  mounted() {
    
    axios
      .get(
        "http://pcapi-xiaotuxian-front-devtest.itheima.net/category/sub/filter?id=" +
          this.$route.query.id
      )
      .then((res) => {
        console.log(res);
        if ((res.data.msg = "操作成功")) {
          this.filter = res.data.result;
          // console.log(this.filter);
        }
      })
      .catch(function (error) {
        //error 捕获错误 axios 发生错误的时候 执行的

        console.log(error);
        //    alert(error.response.data.message)
      });

    window.addEventListener("scroll", this.scrollaxios); //设置屏幕滚动
  },
  methods: {
    scrollaxios() {
      const scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      const clientHeight = document.documentElement.clientHeight;
      const scrollHeight = document.documentElement.scrollHeight;

      if (scrollTop + clientHeight >= scrollHeight) {
        if (this.page > this.page_count) return;
        this.page++;
        axios({
          url: "http://pcapi-xiaotuxian-front-devtest.itheima.net/category/goods/temporary",
          method: "post",
          data: {
            categoryId: "109206009",
            page: this.page,
            pageSize: this.items,
          },
        }).then((res) => {
          //   console.log(res);
          if ((res.data.msg = "操作成功")) {
            this.temporary = res.data.result;

            // console.log(this.temporary);
          }
        });
      }
    },
  },
};
</script>
<style scoped>
.nav {
  width: 100%;
  height: 72px;
}
.sub-filter {
  background: #fff;
  padding: 25px;
  height: 100%;
}
.commonwidth {
  /* 版心 */
  width: 1240px !important;
  margin-left: auto;
  margin-right: auto;
}
.item {
  height: 44.26px;
  display: flex;
}
.itembox {
  display: flex;
}
.head {
  width: 80px;
  color: #999;
  line-height: 40px;
}
.body {
  flex: 1;
}
.ellipsis {
  margin-right: 36px;
  /* transition: all .3s; */
  display: inline-block;
  max-width: 112px;
  line-height: 40px;
  color: #27ba9b;
}
.ell {
  margin-right: 36px;
  /* transition: all .3s; */
  display: inline-block;
  max-width: 112px;
  line-height: 40px;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
}
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sort {
  display: flex;
}
.sort-span {
  height: 30px;
  line-height: 28px;
  border: 1px solid #e4e4e4;
  padding: 0 20px;
  margin-right: 20px;
  color: #999;
  border-radius: 2px;
  position: relative;
  transition: all 0.3s;
}
.xtc {
  margin-left: 20px;
  color: #999;
  float: left;
}
.xtcb {
  margin-left: 20px;
  color: #999;
  float: left;
}
ul {
  height: 11000px;
  padding: 0 5px;
}
ul li {
  margin-right: 20px;
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.goods-item {
  display: block;
  width: 220px;
  padding: 20px 30px;
  text-align: center;
  transition: all 0.5s;
}
img {
  width: 160px;
  height: 160px;
}
.name {
  padding-top: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.desc {
  color: #999;
  height: 29px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.price {
  color: #cf4444;
  font-size: 20px;
}
.active {
  color: #27ba9b;
}
</style>